package com.guigu.controller;

import com.alibaba.druid.pool.WrapperAdapter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guigu.ZhucheService;
import com.guigu.pojo.FrontUser;
import com.guigu.pojo.SysUser;
import com.guigu.vo.Pojo;
import com.guigu.vo.Vo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

@RequestMapping("/zhuche")
@CrossOrigin
@RestController
public class ZhucheController {
    @Autowired
    private ZhucheService zhucheService;
    @Autowired
    private WrapperAdapter wrapperAdapter;

    //图片上传
    @PostMapping("/image")
    @ResponseBody
    public String image(MultipartFile picture) throws IOException {
        System.out.println(picture);
        //获取文件名
        String orgFileName = picture.getOriginalFilename();
        System.out.println(orgFileName);
        //获取文件后缀
        String suffix = orgFileName.substring(orgFileName.lastIndexOf("."));
        //生成新的文件名
        String newFileName = System.currentTimeMillis() + suffix;
        System.out.println(newFileName);
        //上传文件
        File newfile =new File("D:\\study\\files\\fuwuqi\\"+newFileName);
        picture.transferTo(newfile);
        return "http://localhost:11111/images/"+newFileName;
    }
    //注册用户
    @PostMapping("/zhuche1")
    @ResponseBody
    public String zhuche1(@RequestBody FrontUser frontUser) {
        System.out.println(frontUser);
        frontUser.setIsVendor(0);
        frontUser.setIsBusinesses(0);
        zhucheService.save(frontUser);
        return "ok";
    }
    //登录
    @PostMapping("/login")
    @ResponseBody
    public Integer login(@RequestBody FrontUser frontUser) {
        System.out.println(frontUser);
        Integer i = zhucheService.selectByAccount(frontUser);
        System.out.println(i);
        //将i转换为字符串
        return i == null ? 0 : i;
        //如果i等于空值，返回0，否则返回1

    }
    //后台登录
    @PostMapping("/login1")
    @ResponseBody
    public Integer login1(@RequestBody SysUser sysUser) {
        System.out.println(sysUser);
        Integer sysUser1 = zhucheService.selectByAccount1(sysUser);
        System.out.println(sysUser1);
        return sysUser1 == null ? 0 : sysUser1;
    }
    //根据id查询
    @PostMapping("/selectById/{uid2}")
    @ResponseBody
    public SysUser selectById(@PathVariable int uid2) {
        System.out.println("ui"+uid2);
       SysUser sysUser = zhucheService.selectById(uid2);
        System.out.println(sysUser);
        return sysUser;
    }
    @PostMapping("/selectById1/{uid6}")
    public FrontUser selectById1(@PathVariable int uid6){
        System.out.println("uid6"+uid6);
        QueryWrapper<FrontUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",uid6);
        FrontUser frontUser = zhucheService.getOne(queryWrapper);
        System.out.println(frontUser);
        return frontUser;
    }
    @PostMapping("/updateById")
    public String updateById(@RequestBody FrontUser frontUser){
        System.out.println(frontUser);
        zhucheService.updateById(frontUser);
        return "ok";
    }
    //修改密码
@PostMapping("/updatePassword")
    public int updatePassword(@RequestBody FrontUser frontUser){
        System.out.println(frontUser);
    //通过id和密码查询
    if (zhucheService.selectById1(frontUser)==1){
        zhucheService.updatePassword(frontUser);
        System.out.println("密码正确");
        return 200;
    }
    else {
        System.out.println("密码错误");
        return 1;
    }
    }
    @PostMapping("/selectAll")
    public Pojo<FrontUser> selectAll(@RequestBody Vo vo,Integer pageNum,Integer pageSize) {
        System.out.println("进入");
        Pojo<FrontUser> frontUserPojo = zhucheService.selectAll(vo, pageNum, pageSize);
        System.out.println(frontUserPojo);
        return frontUserPojo;
    }
    //删除
    @DeleteMapping("/{userId}")
    public String delete(@PathVariable int userId){
        System.out.println("0999"+userId);
        boolean b = zhucheService.removeById(userId);
        if(b)
            return "ok";
        return "no";
    }
    //根据userId修改
    @PutMapping("/updateById1")
    public String updateById1(@RequestBody FrontUser frontUser){
        System.out.println("修改一"+frontUser);
        zhucheService.updateById9(frontUser);
        return "ok";
    }
    @PutMapping("/updateById2")
    public String updateById2(@RequestBody FrontUser frontUser){
        System.out.println("修改二"+frontUser);
        zhucheService.updateById99(frontUser);
        return "ok";
    }


    //李xx
    //前台根据id查user
    @GetMapping("/One/{uid}")
    @ResponseBody
    public FrontUser One(@PathVariable int uid) {
        FrontUser one = zhucheService.One(uid);
        return one;
    }
}
